import { ConfigService } from '@nestjs/config';
import * as session from 'express-session';
import * as connectRedis from 'connect-redis';
import { RedisCommonService } from '../redis/redis.service';
import { ConfigEnum } from '../enum/config.enum';

export const essionConfig: any = (
  configService: ConfigService,
  redisCommonService: RedisCommonService,
) => {
  const secret =
    configService.get<string>(ConfigEnum.SESSION_SECRET) || 'your-secret-key';

  return session({
    store: new connectRedis.RedisStore({
      client: redisCommonService.getClient(),
    }),
    secret: secret,
    name: 'sessionId',
    resave: true, // 强制保存到仓库
    saveUninitialized: false, // 没有用过的session要不要保存
    cookie: {
      // session 内部也是需要通过cookie实现的
      maxAge: Number(configService.get<string>(ConfigEnum.JWT_EXPIRES_IN)),
      // domain: configService.get<string>(ConfigEnum.SERVER_HOST),
      // domain: 'localhost1sss',
    },
  });
};
